home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / swags-z / sorting.swg / 0007_COUNT1.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  63 lines

  1. {
  2.   ...Well, as Greg Vigneault reminded me, there is a much faster
  3.   method of sorting this sort of data called a "Count" sort. I
  4.   often overlook this method, as it doesn't appear to be a sort
  5.   at all at first glance:
  6. }
  7. Program Count_Sort_Demo;
  8.  
  9. Const
  10.   co_MaxItem = 200;
  11.  
  12. Type
  13.   byar_MaxItem = Array[1..co_MaxItem] of Byte;
  14.   byar_256     = Array[0..255] of Byte;
  15.  
  16. Var
  17.   by_Index   : Byte;
  18.   wo_Index   : Word;
  19.   DataBuffer : byar_MaxItem;
  20.   SortTable  : byar_256;
  21.  
  22. begin
  23.            (* Initialize the pseudo-random number generator.       *)
  24.   randomize;
  25.  
  26.            (* Clear the CountSort table.                           *)
  27.   fillChar(SortTable, sizeof(SortTable), 0);
  28.  
  29.            (* Create random Byte data.                             *)
  30.   For wo_Index := 1 to co_MaxItem do
  31.     DataBuffer[wo_Index] := random(256);
  32.  
  33.            (* Display random data.                                 *)
  34.   Writeln;
  35.   Writeln('RANDOM Byte DATA');
  36.   For wo_Index := 1 to co_MaxItem do
  37.     Write(DataBuffer[wo_Index]:4);
  38.  
  39.            (* CountSort the random data.                           *)
  40.   For wo_Index := 1 to co_MaxItem do
  41.     inc(SortTable[DataBuffer[wo_Index]]);
  42.  
  43.            (* Display the CountSorted data.                        *)
  44.   Writeln;
  45.   Writeln('COUNTSORTED Byte DATA');
  46.   For by_Index := 0 to 255 do
  47.     if (SortTable[by_Index] > 0) then
  48.       For wo_Index := 1 to SortTable[by_Index] do
  49.         Write(by_Index:4)
  50. end.
  51. {
  52.   ...This Type of sort is EXTEMELY fast, even when compared to
  53.   QuickSort, as there is so little data manipulation being done.
  54.  
  55. >BTW, why are there so many different sorting methods?
  56. >Quick, bubble, Radix.. etc, etc
  57.  
  58.   ...Because, Not all data is created equally.
  59.   (ie: Some Types of sorts perform well on data that is very
  60.        random, While other Types of sorts perform well on data
  61.        that is "semi-sorted" or "almost sorted".)
  62.  
  63. }